package com.fengba.nanzhi.tabbar04.wz;

import android.app.Activity;
import android.content.Intent;
import android.media.Image;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.Function;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

public class MyTeamActivity extends AppCompatActivity {
    //用户兴民修改
    FrameLayout user_level_box;
    private TextView My_Team_Number,My_Team_LJNumber,My_Team_toDayNumber,My_Team_NewTeam,My_Team_lv1,My_Team_lv2,My_Team_lv3;
    private String user_id,share_money,headimg;
    private ImageView MyTeamtx;
    private Button My_Team_Btn1,My_Team_Btn2,My_Team_Btn3;
    private ImageButton MyTeamtfh;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wz_activity_my_team);
        user_level_box = findViewById(R.id.Myteam_user_level_box);

        //所有下级数量
        My_Team_Number = findViewById(R.id.My_Team_Number);
        //累计奖励
        My_Team_LJNumber = findViewById(R.id.My_Team_LJNumber);
        //今日奖励
        My_Team_toDayNumber = findViewById(R.id.My_Team_toDayNumber);
        //新增好友
        My_Team_NewTeam = findViewById(R.id.My_Team_NewTeam);
        //等级一
        My_Team_lv1 = findViewById(R.id.My_Team_lv1);
        //等级一
        My_Team_lv2 = findViewById(R.id.My_Team_lv2);
        //等级一
        My_Team_lv3 = findViewById(R.id.My_Team_lv3);
        //用户头像
        MyTeamtx = findViewById(R.id.MyTeamtx);
        //跳转到选项卡
        My_Team_Btn1 = findViewById(R.id.My_Team_Btn1);
        My_Team_Btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyTeamActivity.this,MyTeamLevelActivity.class);
                intent.putExtra("MyLevel_id","1");
                startActivity(intent);
            }
        });
        //跳转到选项卡
        My_Team_Btn2 = findViewById(R.id.My_Team_Btn2);
        My_Team_Btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyTeamActivity.this,MyTeamLevelActivity.class);
                intent.putExtra("MyLevel_id","2");
                startActivity(intent);
            }
        });
        //跳转到选项卡
        My_Team_Btn3 = findViewById(R.id.My_Team_Btn3);
        My_Team_Btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyTeamActivity.this,MyTeamLevelActivity.class);
                intent.putExtra("MyLevel_id","3");
                startActivity(intent);
            }
        });

        //返回按钮
        MyTeamtfh = findViewById(R.id.MyTeamtfh);
        MyTeamtfh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        init();
    }

    public void init(){
        Object user_info = Preferences.getParam(MyTeamActivity.this,"user_info","");
        try {
            JSONObject myJson = new JSONObject(user_info.toString());

            //账号
            user_id = myJson.get("id").toString();
            share_money = myJson.get("share_money").toString();
            headimg = myJson.get("headimg").toString();
            int levelid = Integer.parseInt(myJson.get("level_id").toString());
            createStart(levelid);
            Glide.with(MyTeamActivity.this).load(headimg).into(MyTeamtx);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OKManager.getInstance().type1017(user_id, new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if(jsonObject.get("code").toString().trim().equals("0")) {
                        JSONObject jsob = (JSONObject) jsonObject.get("data");
                        My_Team_Number.setText(jsob.get("AllChildId").toString());
                        My_Team_LJNumber.setText(share_money);
                        My_Team_toDayNumber.setText(jsob.get("share").toString());
                        My_Team_NewTeam.setText(jsob.get("newChild").toString());
                        My_Team_lv1.setText(jsob.get("userRemoveLevel1").toString());
                        My_Team_lv2.setText(jsob.get("userRemoveLevel2").toString());
                        My_Team_lv3.setText(jsob.get("userRemoveLevel3").toString());
                    }else{
                        showToast(MyTeamActivity.this,"网络错误");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String string) {
                showToast(MyTeamActivity.this,"网络错误");
            }
        });
    }

    /**
     * 生成星星
     * @param index
     */
    private void createStart(int index){
        for (int i = 0; i < 4; i++){
            ImageView img = new ImageView(MyTeamActivity.this);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(0,0);
            params.width = Function.dip2px(MyTeamActivity.this,22);
            params.height = Function.dip2px(MyTeamActivity.this,22);
            params.leftMargin = 4+i*Function.dip2px(MyTeamActivity.this,30);
            img.setLayoutParams(params);
            img.setImageDrawable(ContextCompat.getDrawable(MyTeamActivity.this.getApplicationContext(),R.mipmap.img_0022));
            user_level_box.addView(img);
        }

        for (int i = 0; i < index; i++){
            ImageView img = new ImageView(MyTeamActivity.this);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(0,0);
            params.width = Function.dip2px(MyTeamActivity.this,22);
            params.height = Function.dip2px(MyTeamActivity.this,22);
            params.leftMargin = 4+i*Function.dip2px(MyTeamActivity.this,30);
            img.setLayoutParams(params);
            img.setImageDrawable(ContextCompat.getDrawable(MyTeamActivity.this.getApplicationContext(),R.mipmap.img_0023));
            user_level_box.addView(img);
        }
    }
    public static void showToast(final Activity ctx, final String msg) {
        // 判断是在子线程，还是主线程
        if ("main".equals(Thread.currentThread().getName())) {
            Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
        } else {
            // 子线程
            ctx.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
